package alexanderHash;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class MainDynamicHash {
    public static void main(String[] args) throws IOException {
        if (args.length != 1) {																			/*1*/
            System.err.println("Error: Incorrect number of arguments, expected one");
            System.exit(1);
        }

        // Create a new hash with the found array size
        DynamicHash dh = new DynamicHash();

        // Åben filen igen og begynd at tilføje værdier.
        // Track the latest URL.
        BufferedReader infile = new BufferedReader(new FileReader(args[0]));
        String lineRead = infile.readLine();
        String lastURL = "";
        
       
        
        while(lineRead != null) {																		/*2*/
            if((lineRead.length() >= 6 && lineRead.substring(0, 6).equals("*PAGE:")))					/*3*/
                lastURL = lineRead.substring(6);
            else																						
                lineRead = lineRead.toLowerCase();
            	dh.put(lineRead, lastURL);

            lineRead = infile.readLine();
        }
        
        if (lastURL == "") {																			/*1*/
            System.err.println("Error: Incorrect file, expected urls.");
            System.exit(1);
        }
        // The number of elements in the hash is 2 because keys are
        // unique, i.e. it contains two keys: "ITU" and "Peter"
        System.out.println("Number of elements in hash: " + dh.noElements());

        // The load factor is the fraction between the elements stored in the hash
        // and the size of the array: 2 / 20 = 0.1
        System.out.println("Load factor               : " + dh.loadFactor());
        System.out.println("Dynamic hash resized " + dh.noResized() + " times\n");

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Search for: ");
        lineRead = br.readLine();
        lineRead = lineRead.toLowerCase();
        while (!lineRead.isEmpty()) {																	/*4*/
            String urls = dh.getURLs(lineRead);
            
            if (urls.isEmpty())																			/*5*/
                System.out.println("Unable to find word \"" + lineRead + "\"\n");
            else																						
                System.out.println(urls);

            System.out.print("Search for: ");
            lineRead = br.readLine();
        }
    }
}